package org.sang.service;

import org.apache.log4j.Logger;
import org.sang.bean.User;
import org.sang.bean.WeekReport;
import org.sang.common.UserUtils;
import org.sang.mapper.WeekReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by LittleXuan on 2015/10/17.
 */
@Service("weekReportService")
@Transactional
public class WeekReportService  {
	@Autowired
    private WeekReportMapper weekReportMapper;
    SimpleDateFormat birthdayFormat = new SimpleDateFormat("yyyy-MM-dd");
    public List<WeekReport> getWeekReportByPage(Integer page, Integer size, String keywords, Long userId, String beginDateScope) {
        int start = (page - 1) * size;
        Date startBeginDate = null;
        Date endBeginDate = null;
        if (beginDateScope != null && beginDateScope.contains(",")) {
            try {
                String[] split = beginDateScope.split(",");
                startBeginDate = birthdayFormat.parse(split[0]);
                endBeginDate = birthdayFormat.parse(split[1]);
            } catch (ParseException e) {
            }
        }
        return weekReportMapper.getWeekReportByPage(start, size, keywords, userId, startBeginDate, endBeginDate);
    }

    public Long getCountByKeywords(String keywords, Long userId, String beginDateScope) {
        Date startBeginDate = null;
        Date endBeginDate = null;
        if (beginDateScope != null && beginDateScope.contains(",")) {
            try {
                String[] split = beginDateScope.split(",");
                startBeginDate = birthdayFormat.parse(split[0]);
                endBeginDate = birthdayFormat.parse(split[1]);
            } catch (ParseException e) {
            }
        }
        return weekReportMapper.getCountByKeywords(keywords, userId,startBeginDate, endBeginDate);
    }

    public int addWeekReport(WeekReport weekReport) {
            User user= UserUtils.getCurrentUser();
            weekReport.setAuthor(user.getName());
            weekReport.setUserId(user.getId());
            return weekReportMapper.insert(weekReport);
    }

    public boolean delWeekReportById(String ids) {
        String[] split = ids.split(",");
        return weekReportMapper.delWeekReportById(split) == split.length;
    }

	public int updateWeekReport(WeekReport weekReport) {
		// TODO Auto-generated method stub
		return weekReportMapper.updateByPrimaryKeySelective(weekReport);
	}

	public WeekReport getWeekReportById(int weekId) {
		// TODO Auto-generated method stub
		return weekReportMapper.selectByPrimaryKey(weekId);
	}


}
